﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class EShop_ViewSendReceiveMail : System.Web.UI.Page
{
    ManageEmailMarketing MEM = new ManageEmailMarketing();
    UsersRepository URepo = new UsersRepository();
    protected void Page_Load(object sender, EventArgs e)
    {
        HttpCookie _user = Request.Cookies["userinfo"];
        if (Request.Cookies["userinfo"] != null)
        {
            if (_user["username"] != null)
            {
                int UserId = URepo.GetUserIdByName(_user["username"]);
                if (!Page.IsPostBack)
                {
                    GetInbox(UserId);
                    GetOutbox(UserId);
                }
            }
        }
        //else
        //    Response.Redirect("~/Index.aspx");
    }

    private void GetInbox(int UserId)
    {
        grvInbox.DataSource = MEM.GetReceiveMail(UserId);
        grvInbox.DataBind();
    }
    private void GetOutbox(int UserId)
    {
        grvOutbox.DataSource = MEM.GetSendMail(UserId);
        grvOutbox.DataBind();
    }
    protected void grvInbox_RowCommand(object source, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "deleteInbox")
        {
            try
            {
                HttpCookie _user = Request.Cookies["userinfo"];
                int UserId = URepo.GetUserIdByName(_user["username"]);
                MEM.DeleteMail(Guid.Parse(e.CommandArgument.ToString()));
                GetInbox(UserId);
            }
            catch (Exception)
            {
                Response.RedirectToRoute("Error");
            }
        }
    }
    protected void grvOutbox_RowCommand(object source, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "deleteOutbox")
        {
            try
            {
                HttpCookie _user = Request.Cookies["userinfo"];
                int UserId = URepo.GetUserIdByName(_user["username"]);
                MEM.DeleteMail(Guid.Parse(e.CommandArgument.ToString()));
                GetOutbox(UserId);
            }
            catch (Exception)
            {
                Response.RedirectToRoute("Error");
            }
        }
    }

    protected void grvInbox_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        try
        {
            this.grvInbox.PageIndex = e.NewPageIndex;
            HttpCookie _user = Request.Cookies["userinfo"];
            if (Request.Cookies["userinfo"] != null)
            {
                if (_user["username"] != null)
                {
                    int UserId = URepo.GetUserIdByName(_user["username"]);
                    GetInbox(UserId);
                }
            }
        }
        catch (Exception)
        {
            Response.RedirectToRoute("Error");
        }
    }

    public string MailDetail(string Id)
    {
        return ResolveUrl("~/LetterDetail/" + Id + ".html");
    }

    //[DirectMethod]
    //public void ComposeMail(object sender, EventArgs e)
    //{
    //    Response.Redirect(ResolveUrl("~/Compose.html"));
    //}
}